package src.com.qf.controller;

import src.com.qf.entity.Good;
import src.com.qf.entity.Page;
import src.com.qf.service.GoodService;
import src.com.qf.service.impl.GoodServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/good")
public class GoodController extends BaseController {
    GoodService goodService = new GoodServiceImpl();

    /**
     * 分类查询商品列表
     * @param request
     * @param response
     * @return
     */
    public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response){

        String strTypeId = request.getParameter("typeId");
        String strPageIndex = request.getParameter("pageIndex");
        Integer typeId=0;
        if(strTypeId==null){
            //默认查询手机下面的商品
            typeId=1;
        }else{
            typeId=Integer.parseInt(strTypeId);
        }
        int pageIndex=0;
        if(strPageIndex==null){
            pageIndex=1;
        }else{
            pageIndex=Integer.parseInt(strPageIndex);
        }
        Page page = new Page(pageIndex);
        List<Good> list = goodService.getGoodsListByTypeId(typeId,page);
        System.out.println(list);
        request.setAttribute("goods",list);//展示数据
        request.setAttribute("page",page);//用于制作分页
        request.setAttribute("typeId",typeId);//当用户点击分页相关按钮是

        return "forward:index.jsp";
    }

    /**
     * 根据商品id，查询商品详情
     * @param request
     * @param response
     * @return
     */
    public String showGoodDetail(HttpServletRequest request, HttpServletResponse response){
        String strGoodid = request.getParameter("goodId");
        Good good = goodService.showGoodDetail(Integer.parseInt(strGoodid));
        request.setAttribute("good",good);

        return "forward:goodsDetail.jsp";
    }
}
